const Router = require('koa-router');
const router = new Router();
const {db,QueryTypes } = require("../../models/db");
// 获取页面设置
router.post('/subject/del', async ctx => {
    let tenantId = ctx.user.tenantId;
    let id = ctx.request.body.id;
    if(!id) return ctx.err('不能删除')
    let model = await db.models['category'].findOne({
        where: {
            tenantId,
            id
        }
    })
    if(!model) return ctx.err('没找到科目')
    if(tenantId !== 1) {
        if(model.isInit === 1) return ctx.err('不能删除默认科目')
    }
    let card = await db.query(`select count(*) total from card_info where subjectId=${id}`, {type: QueryTypes.SELECT, plain: true})
    if(card.total > 0) {
        return ctx.err('已使用科目不能删除')
    }
    await model.destroy();
    return ctx.suc('已删除');
})


module.exports = router;